package rdp;

import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;


public class LSNMain implements Listener {

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        e.setJoinMessage(null);
        RDPUtil.NOTICE("§e玩家 §f" + e.getPlayer().getName() + " §e上线了.");
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
        e.setQuitMessage(null);
        RDPUtil.NOTICE("§e玩家 §f" + e.getPlayer().getName() + " §e下线了.");
    }

    @EventHandler
    public void onChat(AsyncPlayerChatEvent e) {
        e.setMessage(RDPUtil.FORMAT(e.getMessage()));
        e.setFormat("§7[§a" + e.getPlayer().getLevel() + "§f级§7] <§f%1$s§7> §f%2$s");
    }

    @EventHandler
    public void onClick(InventoryClickEvent e) {
        Inventory inv = e.getInventory();
        if (inv.contains(Material.BARRIER)) {
            Player player = (Player) e.getWhoClicked();
            net.minecraft.server.v1_15_R1.ItemStack nms = CraftItemStack.asNMSCopy(e.getCurrentItem());
            if (nms.hasTag()) {
                player.closeInventory();
                if (e.isLeftClick() && nms.getTag().hasKey("Left"))
                    RDPUtil.EXECUTE(player, nms.getTag().getString("Left"));
                if (e.isRightClick() && nms.getTag().hasKey("Right"))
                    RDPUtil.EXECUTE(player, nms.getTag().getString("Right"));
            }
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPickup(EntityPickupItemEvent e) {
        if (e.getEntity() instanceof Player) {
            Player player = (Player) e.getEntity();
            if (!player.isSneaking())
                e.setCancelled(true);
        }
    }
}
